package com.example.play.service.impl;


import com.example.play.entity.Food;
import com.example.play.entity.Scenic;
import com.example.play.request.FoodPageReq;
import com.example.play.request.FoodReq;
import com.example.play.dao.FoodMapper;
import com.example.play.service.FoodService;
import cn.hutool.core.bean.BeanUtil;
import com.api.core.exception.ServiceException;
import com.api.core.result.PageView;
import com.api.core.util.PageUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.play.util.LngLatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.Optional;

/**
 * 美食(Food)表控制层
 *
 * @author makejava
 * @since 2022-02-26 13:44:35
 */
@Service("FoodService")
public class FoodServiceImpl extends PageUtils implements FoodService {

    @Autowired
    private FoodMapper foodMapper;

    /**
     * 修改数据
     *
     * @param food 实例对象
     * @return 实例对象
     */
    @Override
    public Food update(Food food) {
        foodMapper.updateById(food);
        return foodMapper.selectById(food.getId());
    }

    /**
     * 添加实现
     *
     * @param foodReq !{tableInfo.comment}参数对象
     * @return Food
     */
    @Override
    public Food addFood(FoodReq foodReq) {
        Optional.ofNullable(foodReq).orElseThrow(() -> new ServiceException("请求参数不能为空"));
        Food food = new Food();
        BeanUtil.copyProperties(foodReq, food);
        food.setStatus(1);
        food.setCreatedTime(LocalDateTime.now());
        foodMapper.insert(food);
        return food;
    }

    /**
     * 查询美食 列表
     *
     * @param foodReq 编号
     * @return Food
     */
    @Override
    public PageView findFoodList(FoodPageReq foodReq) {
        Optional.ofNullable(foodReq).orElseThrow(() -> new ServiceException("请求参数不能为空"));
        LambdaQueryWrapper<Food> lambdaQuery = getFoodQuery(foodReq);
        Page<Food> page = new Page<>(foodReq.getNowPage(), foodReq.getPageSize());
        Page foodList = foodMapper.selectPage(page, lambdaQuery);
        return getPageView(foodList);
    }

    /**
     * 查询美食 详情
     *
     * @param foodId food编号
     * @return Food
     */
    @Override
    public Food getFoodById(Integer foodId) {
        return foodMapper.selectById(foodId);
    }

    /**
     * 删除美食
     *
     * @param foodId food编号
     * @return Food
     */
    @Override
    public boolean deleteFood(Integer foodId) {
        Food food = Optional.ofNullable(foodMapper.selectById(foodId))
                .orElseThrow(() -> new ServiceException("food不存在！"));
        ;
        return foodMapper.updateById(food) > 0;
    }


    /**
     * 美食 条件构造器
     *
     * @param foodReq 美食 查询条件
     * @return Wrappers
     */
    public LambdaQueryWrapper<Food> getFoodQuery(FoodPageReq foodReq) {
        LambdaQueryWrapper<Food> lambdaQuery = Wrappers.lambdaQuery();
        Map map = LngLatUtil.getAround(foodReq.getLat(), foodReq.getLng(), foodReq.getDistanceRange());
        if (!"".equals(foodReq.getFoodName())) {
            lambdaQuery.like(Food::getFoodName, foodReq.getFoodName());
        }
        if (!foodReq.getLat().equals("")) {
            lambdaQuery.le(Food::getLng, map.get("maxLng"));
            lambdaQuery.ge(Food::getLng, map.get("minLng"));
            lambdaQuery.le(Food::getLat, map.get("maxLat"));
            lambdaQuery.ge(Food::getLat, map.get("minLat"));
        }
        lambdaQuery.eq(Food::getStatus, 1);
        return lambdaQuery;
    }
}
